//
//
//  @ Project : AgriMarketSystem
//  @ Date : 18/10/2013
//  @ Author : NTT
//
//

using System;
using System.Linq;
using System.Collections.Generic;
namespace AgriMarketService
{
    public class SellArticle : Article
    {
        public SellArticle()
            : base()
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            this.type = (from tempArticleType in db.Tbl_TypeOfArticles
                         where tempArticleType.TypeId == 1
                         select new TypeOfArticle(tempArticleType.TypeId, tempArticleType.Type)).FirstOrDefault();
        }
        public SellArticle(int articleId,
            string topic,
            //int productId,
            Product product,
            int quantity,
            Unit unit,
            decimal price,
            string description,
            DateTime postedDate,
            string productImage,
            string authorId
            //UserAccount author
            )
            : base()
        {
            this.articleId = articleId;
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            this.type = (from tempArticleType in db.Tbl_TypeOfArticles
                         where tempArticleType.TypeId == 1
                         select new TypeOfArticle(tempArticleType.TypeId, tempArticleType.Type)).FirstOrDefault();
            this.topic = topic;
            //this.productId = productId;
            this.Product = product;
            this.quantity = quantity;
            this.unit = unit;
            this.price = price;
            this.description = description;
            this.postedDate = postedDate;
            this.productImage = productImage;
            this.authorId = authorId;
            //this.Author = author;
        }
        public static List<Article> GetAllNewArticles()
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Article> result = (from tempArticle in db.Tbl_Sell_Buy_Articles
                                    join tempType in db.Tbl_TypeOfArticles on tempArticle.TypeId equals tempType.TypeId
                                    join tempUnit in db.Tbl_Units on tempArticle.UnitId equals tempUnit.UnitId
                                    where tempArticle.TypeId == 1
                                    select new Article(tempArticle.ArticleId,
                                        new TypeOfArticle(tempType.TypeId, tempType.Type),
                                        tempArticle.Topic,
                                        Product.GetProductById(tempArticle.ProductId),
                                        tempArticle.Quantity,
                                        new Unit(tempUnit.UnitId, tempUnit.UnitName),
                                        tempArticle.Price,
                                        tempArticle.Description,
                                        tempArticle.PostedDate,
                                        tempArticle.ProductImage,
                                        tempArticle.AuthorId
                                        )).ToList();
            return result;
        }
    }
}